﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using Framework.Common;

namespace Framework.Serializer
{
    public abstract class SerializeBase<To>
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public abstract To Serialize(object obj);
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public abstract T DeSerialize<T>(To source);
        /// <summary>
        /// serialize to file
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public virtual bool SerializeToFile(object obj, string fullPath)
        {
            try
            {
                FileHelper.CreateDir(fullPath);
                using (FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    To to = Serialize(obj);
                    ActionSerializeToFile(fs, to);
                    return true;
                }
            }
            catch (Exception e)
            {
                LogHelper.Fatal("序列化出错", e);
                return false;
            }
        }
        /// <summary>
        /// deserialize from file
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public virtual T DeSerializeFromFile<T>(string fullPath)
        {
            try
            {
                using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    To obj = FuncDeserializeFromFile(fs);
                    return DeSerialize<T>(obj);
                }
            }
            catch (Exception e)
            {
                LogHelper.Fatal("反序列化出错", e);
                return default(T);
            }
        }

        protected abstract To FuncDeserializeFromFile(FileStream fs);

        protected abstract void ActionSerializeToFile(FileStream fs, To to);
    }

    /// <summary>
    /// 转化类型为string
    /// </summary>
    public abstract class StringSerializeBase : SerializeBase<string>
    {
        /// <summary>
        /// 
        /// </summary>
        protected override string FuncDeserializeFromFile(FileStream fs)
        {
            using (var reader = new StreamReader(fs))
            {
                return reader.ReadToEnd();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void ActionSerializeToFile(FileStream fs, string to)
        {
            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.Write(to);
            }
        }
    }

    /// <summary>
    /// 转化类型为byte[]
    /// </summary>
    public abstract class BytesSerializeBase : SerializeBase<byte[]>
    {
        /// <summary>
        /// 
        /// </summary>
        protected override byte[] FuncDeserializeFromFile(FileStream fs)
        {
            return fs.ToBytes();
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void ActionSerializeToFile(FileStream fs, byte[] to)
        {
            fs.Write(to, 0, to.Length);
        }
    }
}
